<template>
  <div class="connector-app condition-app" @click="open">
    <div class="header">{{ $t('common.base.condition') }}{{ ruleNameDisplayText?'（'+ruleNameDisplayText+'）':'' }}</div>
    <div class="condition-list">
      <div
        class="condition-empty"
        v-if="(!app.data.conditionList || !app.data.conditionList.length || !conditionList.length) && app.data.convertType !== 'convertCode'"
      >
        {{ $t('common.connector.trigger.validate.text17') }}
      </div>
      <template v-else-if="app.data.convertType === 'fieldMapping'">
        <div
          v-for="(condition, conditionIndex) in app.data.conditionList"
          :key="conditionIndex"
          class="condition-box"
        >
          <div class="condition-header">
            {{ $t('common.paas.view.designer.workFlow.conditionGroup') }} {{ conditionIndex+1 }}
          </div>
          <div
            class="condition-content"
            v-if="condition && condition.length > 0"
          >
            <div
              v-for="(item, index) in condition"
              :key="index">
              <div class="rule-text">
                <span class="and-text">
                  {{ index == 0 ? $t('common.connector.text.when') : $t('common.connector.text.and') }}&nbsp;
                </span>
                <span v-if="item.field">
                  {{ item.field.cnName || item.field.displayName }}
                </span>
                <span class="operator-text">
                  &nbsp;{{ transformOperator(item.operator) }}&nbsp;
                </span>
                <span>{{ getFixedValue(item) }}</span>
              </div>
            </div>
          </div>
        </div>
      </template>
      <template v-else>
        <div class="condition-box">
          <div class="code-content">{{ app.data.convertCode }}</div>
        </div>
      </template>
    </div>
  </div>
</template>
<script>
import { message } from '@src/util/message'
import { getFixedValue } from '@src/modules/connector/util';
import { t } from '@src/locales'
import { ConnectorConditionTypeEnum } from 'src/modules/connector/model/enum'
import { isString } from 'lodash'
export default {
  name: 'condition',
  props: {
    nodeConfig: {
      type: Object,
      default: () => ({}),
    },
    moduleList: {
      type: Array,
      default: () => [],
    },
    parent:{
      type:Object,
      default:()=>({})
    }
  },
  computed:{
    parentApp(){
      return this.parent.isTrigger?this.parent.getParent().data:this.parent.data;
    },
    ruleName() {
      return this.app?.data?.name || ''
    },
    ruleNameDisplayText() {
      return this.ruleName || this.$t('common.base.rulerName')
    },
    conditionList(){
      return this.app.data?.conditionList.filter(group=>{
        return group.filter(item=>{
          return (item.rightFieldValue || item.value || item.operator == 'EMPTY' || item.operator == 'NOT_EMPTY')
        }).length > 0
      })
    }
  },
  watch: {
    nodeConfig: {
      handler(newVal) {
        this.app = newVal;
      },
      deep: true,
      immediate: true,
    },
    // 不知道干啥用的 先注释了
    // parentApp:{
    //   handler(newVal) {
    //     if(this.app.isEdit){
    //       this.app.isEdit = false
    //     }else{
    //       this.app.data.conditionList = [[]]
    //     }
    //     this.$emit('update:nodeConfig', this.app)
    //   },
    //   deep: true,
    //   immediate: true
    // }
  },
  data() {
    return {
      app: this.nodeConfig,
      operatorTypes: {
        EQ: t('common.base.equal'),
        NOT_EQ: t('common.base.notEqual'),
        BETWEEN: t('common.base.between'),
        CONTAINS: t('common.base.include'),
        NOT_CONTAINS: t('common.base.exclude'),
        NOT_BETWEEN: t('common.base.notBetween'),
        MORE: t('common.base.greaterThan'),
        LESS: t('common.base.lessThan'),
        EMPTY: t('common.base.isNull'),
        NOT_EMPTY: t('common.base.isNotNull'),
        EARLY: t('common.base.earlyThan'),
        LATE: t('common.base.lateThan'),
        LOCATED: t('common.base.located'),
        NOT_LOCATED: t('common.base.notLocated')
      },
    };
  },
  methods: {
    open(){
      // 如果父节点不是消息节点且父节点存在code属性，表示为条件上面为代码，则打开
      if (this.parent.type !== 4 && Reflect.has(this.parentApp, 'code')) {
        this.$emit('open')
        return
      }
      if(this.parent.type !== 4 && !this.parentApp?.bizTypeId){
        return message.error(this.$t('common.connector.validate_rule.selectPreApp'))
      }
      this.$emit('open')
    },
    getFromAppFieldValue(value) {
      if(isString(value)) {
        value = JSON.parse(value)
      }
      return `${value.toBizTypeName || ''}-${value?.response?.cnName || ''}` || ''
    },
    getFixedValue(val){
      let txt = ''
      switch (val.condition) {
        case ConnectorConditionTypeEnum.FixedValue:
          if(!val.rightFieldValue && !val.value) return ''
          if(val.operator == 'EMPTY' || val.operator == 'NOT_EMPTY') return ''
          if(!val.field) return this.$t('common.connector.fixedValue')
          if(!val.field.fieldType){
            val.field.fieldType = val.field?.formType || ''
          }
          txt = `${this.$t('common.connector.fixedValue')} ${getFixedValue(val)}`
          break;

        case ConnectorConditionTypeEnum.AppFormField:
          txt = `${this.$t('common.connector.otherFormValue')} ${this.getFromAppFieldValue(val?.value || '')}`
          break
        default:
          break
      }
      return txt
    },
    transformOperator(type){
      return this.operatorTypes[type?.toUpperCase()]
    }
  },
  components: {},
};
</script>
